package org.wzq.dao;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.wzq.biz.Topic;

import java.util.List;

/**
 * project: spring-mvc-forum
 * ate: 14-9-17
 * author: wzq
 * description: TODO
 */
@Repository
@SuppressWarnings({"unchecked", "rawtypes"})
public class TopicDao {
    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    public List<Topic> allTopinInSection(int sectionId) {
        String hql = "from Topic where sectionId=?";
        Query query = sessionFactory.getCurrentSession().createQuery(hql).setInteger(0, sectionId);
        List<Topic> list = query.list();
        for (Topic topic : list) {
            System.out.println("aUser = " + topic);
        }
        return list;
//        String hql = "from Topic where sectionId=" + sectionId;
//        List<Topic> list = sessionFactory.getCurrentSession().createQuery(hql).list();
//        for (Topic topic : list) {
//            System.out.println("aUser = " + topic);
//        }
//        return list;
    }

    @Transactional
    public List<Topic> allTopinReply(int topicId) {

        String hql = "from Topic where topicId=?";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        query.setInteger(0, topicId);
        List<Topic> list = query.list();
        return list;

//        String hql = "from Topic where topicId=" + topicId;
//        List<Topic> list = sessionFactory.getCurrentSession().createQuery(hql).list();
//        for (Topic topic : list) {
//            System.out.println("aUser = " + topic);
//        }
//        return list;
    }

    @Transactional
    public Topic getTopic(String topicId) {
        String hql = "from Topic where topicId=?";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        query.setString(0, topicId);
        return (Topic) query.uniqueResult();
    }

    @Transactional
    public void saveOrUpdate(Topic topic) {
        sessionFactory.getCurrentSession().saveOrUpdate(topic);
    }
}
